package cn.wolfcode.controller;

import java.util.Set;
import java.util.TreeSet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import cn.wolfcode.utils.AccessTokenUtil;
import cn.wolfcode.utils.JAXBUtil;
import cn.wolfcode.utils.SecurityUtil;
import cn.wolfcode.utils.UrlUtil;
import cn.wolfcode.vo.AccessTokenVo;
import cn.wolfcode.vo.WeChatRequest;
import cn.wolfcode.vo.WeChatResponse;

@Controller
public class WechatController {

	@Value(value="${wechat.token}")
	private String token;
	
	@Value(value="${wechat.apid}")
	private String appid;
	
	@Value(value="${wechat.appsecret}")
	private String appsecret;
	
	@Autowired
	private RestTemplate restTemplate;
	
	@Autowired
	private AccessTokenUtil accessTokenUtil;
	
	/**
	 * 提供一个给微信公众平台调用的接口
	 */
	
	@RequestMapping(value="/wechat",method=RequestMethod.GET)
	@ResponseBody
	public String wechat(String signature,String timestamp,String nonce,String echostr) {
		StringBuilder sb = new StringBuilder();
		Set<String> set = new TreeSet<>();
		set.add(token);
		set.add(timestamp);
		set.add(nonce);
		for(String string : set) {
			sb.append(string);
		}
		String sha1Str = SecurityUtil.SHA1(sb.toString());
		if(sha1Str.equals(signature)) {
			return echostr;
		}
		return "";
	}
	
	@RequestMapping(value="/wechat",method=RequestMethod.POST)
	@ResponseBody
	public String wechat(@RequestBody WeChatRequest request) {
		
		WeChatResponse response = new WeChatResponse();
		response.setToUserName(request.getFromUserName());
		response.setFromUserName(request.getToUserName());
		response.setCreateTime(request.getCreateTime());
		response.setMsgType("text");
		if(request.getMsgType().equals("text")) {
			System.out.println(request.getMsgType());
			System.out.println(request.getContent());
			response.setContent("你好。。。。");
		}else if(request.getMsgType().equals("event")) {
			if(request.getEvent().equals("subscribe")) {
				System.out.println(request.getMsgType());
				System.out.println(request.getFromUserName()+"关注了");
				response.setContent("谢谢订阅");
			}else if(request.getEvent().equals("CLICK")) {
				if(request.getEventKey().equals("key1")) {
					response.setContent("手机性能不够，玩不了啦");
				}
			}
		}
		String responseXml = JAXBUtil.convertToXml(response);
		System.out.println(responseXml);
		return responseXml;
	}
	
	/**
	 * 自定义菜单
	 */
	@RequestMapping(value="/createMeun")
	@ResponseBody
	public String createMeun() {
		
		/**
		 * 1、调用获取access_token的接口需要消耗时间
		 * 2、access_token存在有效时间（7200秒）
		 */
		
		System.out.println(accessTokenUtil.getAccessToken());
		String result = restTemplate.postForObject(UrlUtil.MENU_CREATE_URL.replace("ACCESS_TOKEN", accessTokenUtil.getAccessToken()), "{\"button\":[{\"type\":\"click\",\"name\":\"今日头条\",\"key\":\"V1001_TODAY_MUSIC\"},{\"name\":\"菜单\",\"sub_button\":\r\n" + 
				"\r\n" + 
				"[{\"type\":\"view\",\"name\":\"搜索\",\"url\":\"http://www.soso.com/\"},{\"type\":\"click\",\"name\":\"赞一下我\r\n" + 
				"\r\n" + 
				"们\",\"key\":\"V1001_GOOD\"}]}]}", String.class);
		
		return result;
	}
	
}









